#@author Devdoer<devdoer@gmail.com>

from anole  import AnoleError,AnoleLog
import threading,time,sys
		
class Crawler:
	def __init__(self,parts,co=1):
		self.__cocurrency=co

		usp=parts['USP']
		fetch=parts['FETCHER']
		app=parts['APP']
		try:
			dns=parts['DNS']
		except KeyError:
			#from anole.core.dns import SimpleDns
			dns=None
			parts['DNS']=None
		self.__cores=(usp,dns,fetch,app)
		#self.__cores=(usp,fetch,app)

		self.__parts= parts
		self.__stop=True
		self._log=AnoleLog().log

		
			
	def run(self):
		self.__stop=False

		#ok,crawl in sub threads
		t=threading.Thread(target=self._work_in_child_thread)
		t.setDaemon(True)
		t.start()
		
		
		time.sleep(1)

		self._work_in_main_thread()
		
	def _work_in_main_thread(self):
		CHECK_PERIOD=2#2s
		try:
			while True:
				time.sleep(CHECK_PERIOD)
				#do something

				if self.__stop: 
					self._log('stoping crawler...',AnoleLog.LEVEL_INFO)
					if self.__stopT>0:
						self._log('waiting child threads for %ds...'%self.__stopT ,AnoleLog.LEVEL_INFO)
						time.sleep(self.__stopT)
					self._log('exit',AnoleLog.LEVEL_INFO)
					sys.exit(0)#main thread exit cause process exit


		except KeyboardInterrupt:
			print 'caught keyborad interrupt...'
			pass
			
		
	def _work_in_child_thread(self,):
		usp,dns,fetcher,app=self.__cores
		fetcher.set_usp(usp)
		fetcher.set_handler(app)
		fetcher.set_dns(dns)
		fetcher.set_cocurrency(self.__cocurrency)
		fetcher.run()	
	def get_part(self,name):
		return self.__parts[name]
	def stop(self,t=0):
		self.__stopT=t
		self.__stop=True


